package com.gxa.dr2.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    /**
     * 前置处理
     * @param request
     * @param response
     * @param handler
     * @return  true: 放行  false: 拦截
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 先获取session
        HttpSession session = request.getSession();
        // 再获取session保存的数据
        String emplName = (String) session.getAttribute("emplName");
        // 判断值是否存在
        if (emplName == null) {
            // 说明没有登录
            // 没有登录， 重定向到登录页面
            response.sendRedirect("/login/page");
            return false;
        }
        // 登录过，直接放行
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
